#include "metaFile.h"
//  mysql连接
int mysqlConnect(MYSQL **mysql){
    *mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(*mysql, "localhost", "root", "123", "cpp66th", 0, NULL, 0);
    if(ret == NULL){
        printf("connect error!\n");
        mysql_close(*mysql);
        return -1;
    }
    return 0;
}

// 把结构体中的有效数据插入到文件元数据表中
int insertDataToFileTable(MYSQL *mysql, fileMeta_t *meta){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }
    //  先检查要插入的目录项是否在表中已经存在了
    char *query1 = (char *)malloc(QUERYLEN);
    if(!query1){
        perror("malloc failed");
        return -1;
    }
    if(meta->hashName[0] == '\0'){
        snprintf(query1, QUERYLEN,
                "SELECT COUNT(*) FROM fileMetaDataTable WHERE "
                "fileName = '%s' AND userName = '%s' AND parentId = '%d' AND hashName IS NULL AND fileType = 'd' AND filePath = '%s'",
                meta->fileName, meta->userName, meta->parentId, meta->filePath);
    }else{
        snprintf(query1, QUERYLEN,
                "SELECT COUNT(*) FROM fileMetaDataTable WHERE "
                "fileName = '%s' AND userName = '%s' AND parentId = '%d' AND hashName = '%s' AND fileType = 'f' AND filePath = '%s'",
                meta->fileName, meta->userName, meta->parentId, meta->hashName, meta->filePath);
    }
    if(mysql_query(mysql, query1) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    printf("执行的 SQL: %s\n", query1);
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(result);
        return -1;
    }
    int count = atoi(row[0]);
    mysql_free_result(result);
    free(query1);
    if(count > 0){
        printf("要插入的目录项已经存在了，不允许重复插入!\n");
        return -1;
    }

    // 把结构体的内容写入到SQL语句
    char *query2 = (char *)malloc(QUERYLEN);
    if(meta->hashName[0] == '\0'){
        snprintf(query2, QUERYLEN,
                "INSERT INTO fileMetaDataTable "
                "(filename, username, parentId, hashName, fileType, filePath) "
                "VALUES ('%s', '%s', '%d', NULL, 'd', '%s')",
                meta->fileName, meta->userName, meta->parentId, meta->filePath);
    }else{
        snprintf(query2, QUERYLEN,
                "INSERT INTO fileMetaDataTable "
                "(filename, username, parentId, hashName, fileType, filePath) "
                "VALUES ('%s', '%s', '%d', '%s', 'f', '%s')",
                meta->fileName, meta->userName, meta->parentId, meta->hashName, meta->filePath);
    }

    if(mysql_query(mysql, query2) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    //printf("执行的 SQL: %s\n", query2);
    free(query2);
    return 0;
}

// 根据id去删除文件元数据表中的表项
int deleteDataFromFileTable(MYSQL *mysql, int fid){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }
    // 先检查fid是否在文件元数据表中存在
    char *query1 = (char *)malloc(QUERYLEN);
    snprintf(query1, QUERYLEN, "SELECT * FROM fileMetaDataTable WHERE fid='%d'", fid);
    if(mysql_query(mysql, query1) == 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    printf("执行的 SQL: %s\n", query1);
    free(query1);
    // 执行fid对应的表项删除操作
    char *query2 = (char *)malloc(QUERYLEN);
    snprintf(query2, QUERYLEN, "DELETE FROM fileMetaDataTable WHERE fid = %d", fid);
    if(mysql_query(mysql, query2) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    printf("执行的 SQL: %s\n", query2);
    free(query2);
    return 0;
}
// 检查文件路径是否存在 path是已经处理路径格式的路径
int checkFilePathExists(MYSQL *mysql, char *filePath, char *userName){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }
    //  执行检查文件元数据表中是否存在目标路径
    char *query = (char *)malloc(QUERYLEN);
    snprintf(query, QUERYLEN, "SELECT COUNT(*) FROM fileMetaDataTable WHERE filePath='%s' AND userName='%s'", filePath, userName);

    if(mysql_query(mysql, query) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    printf("执行的 SQL: %s\n", query);
    // 统计检查路径后得到的路径数目
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(result);
        return -1;
    }
    int count = atoi(row[0]);
    mysql_free_result(result);
    free(query);
    if(count > 0){
        printf("文件路径存在\n");
        return 0;
    }else{
        printf("文件路径不存在\n");
        return -1;
    }
}
// 检查目录路径是否存在 path是已经处理路径格式的路径
int checkDirPathExists(MYSQL *mysql, char *filePath, char *userName){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }
    //  执行检查文件元数据表中是否存在目标路径
    char *query = (char *)malloc(QUERYLEN);
    snprintf(query, QUERYLEN, "SELECT COUNT(*) FROM fileMetaDataTable WHERE fileType = 'd' AND filePath='%s' AND userName='%s'", filePath, userName);

    if(mysql_query(mysql, query) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    printf("执行的 SQL: %s\n", query);
    // 统计检查路径后得到的路径数目
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(result);
        return -1;
    }
    mysql_free_result(result);
    free(query);
    int count = atoi(row[0]);
    if(count == 0){
        printf("目录不存在!\n");
        return -1;
    }

    return 0;
}
// 检查hashName是否存在（给服务端用）,存在返回0，不存在返回-1
int checkHashNameExists(MYSQL *mysql, char *hashName){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }

    //  检查哈希值对应的表项的数目
    char *query = (char *)malloc(QUERYLEN);
    snprintf(query, QUERYLEN, "SELECT COUNT(*) FROM fileMetaDataTable WHERE hashName='%s'", hashName);

    if(mysql_query(mysql, query) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    printf("执行的 SQL: %s\n", query);
    //  统计SQL语句执行后得到的表项数目
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(result);
        return -1;
    }
    int count = atoi(row[0]);
    mysql_free_result(result);
    free(query);
    if(count > 0){
        printf("hashName已存在\n");
        return 0;
    }else{
        printf("hashName不存在\n");
        return -1;
    }
}

//  根据路径寻找fid
int getFidByFilepath(MYSQL *mysql, char *filePath, char *userName, int *fid){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }

    char *query = (char *)malloc(QUERYLEN);
    snprintf(query, QUERYLEN, "SELECT fid FROM fileMetaDataTable WHERE filePath='%s' AND userName='%s'", filePath, userName);
    if(mysql_query(mysql, query) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }

    printf("执行的 SQL: %s\n", query);
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(result);
        return -1;
    }
    printf("row = %s\n", row[0]);
    *fid = atoi(row[0]);
    mysql_free_result(result);
    free(query);
    return 0;
}


//  根据fid获取filePath
int getPathByFid(MYSQL *mysql, int fid, char *filePath){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }

    char *query = (char *)malloc(QUERYLEN);
    snprintf(query, QUERYLEN, "SELECT * FROM fileMetaDataTable WHERE fid='%d'", fid);
    if(mysql_query(mysql, query) != 0){
        printf("根据fid查询内容时SQL语句执行错误!\n");
        return -1;
    }
    printf("执行的 SQL: %s\n", query);
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(result);
        return -1;
    }
    strcpy(filePath, row[6]);
    mysql_free_result(result);
    free(query);
    return 0;
}
//  根据pathId获取userName
int getUsernameByFid(MYSQL *mysql, int pathId, char *userName){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }

    char *query = (char *)malloc(QUERYLEN);
    snprintf(query, QUERYLEN, "SELECT userName FROM fileMetaDataTable WHERE fid = %d", pathId);
    if(mysql_query(mysql, query) != 0){
        printf("根据fid查询内容时SQL语句执行错误!\n");
        return -1;
    }
    printf("执行的 SQL: %s\n", query);
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(result);
        return -1;
    }
    strcpy(userName, row[0]);
    mysql_free_result(result);
    free(query);
    return 0;
}

int getUserRootPathIdByUsername(MYSQL *mysql, char *userName){
    if(!mysql){
        printf("数据库未连接!\n");
        return -1;
    }
    char *query = (char *)malloc(QUERYLEN);
    snprintf(query, QUERYLEN, "SELECT fid FROM fileMetaDataTable WHERE userName='%s' AND parentId=-1",userName);
    if(mysql_query(mysql,query) != 0){
        printf("SQL 错误: %s\n", mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res = mysql_store_result(mysql);
    if(res == NULL){
        printf("查询结果为空!\n");
        return -1;
    }
    MYSQL_ROW row = mysql_fetch_row(res);
    if(row == NULL){
        printf("未找到匹配记录!\n");
        mysql_free_result(res);
        return -1;
    }
    int rootId = atoi(row[0]);
    mysql_free_result(res);
    free(query);
    return rootId;
}
